;
(function() {
	"use strict";
	jQuery.fn.extend({
		slideRotary(options) {
			// obj
			var obj = {
				imgs: options.imgs,
				left: options.left,
				right: options.right,
				autoplay: options.autoplay,
				duration: options.duration,
				currentIndex: options.currentIndex,
				point: options.point,
				autoTime: options.autoTime,
				timer: null,
				imgWidth: options.imgs.width(),
				lastTime: 0
			}

			//可选参数
			obj.autoplay = obj.autoplay === false ? false : true;
			obj.duration = obj.duration || 300;
			obj.currentIndex = obj.currentIndex || 0;
			obj.point = obj.point || false;
			obj.autoTime = obj.autoTime || 2000;

			var len = obj.imgs.length;
			var prevIdx = null;
			var nextIdx = null;
			
			//自动滑动
			autoPlay();

			//左点击
			obj.right.on("click", function() {
				var nowTime = Date.now();
				if (nowTime - obj.lastTime > 600) {

					prevIdx = obj.currentIndex;
					obj.currentIndex++;
					if (obj.currentIndex == len) {
						obj.currentIndex = 0;
					}
					nextIdx = obj.currentIndex;
					obj.lastTime = nowTime;

					obj.rightSlide();

					changeLtl(obj.currentIndex)
					
					clearInterval(obj.timer);
					autoPlay();
				}
			})

			//产生右滑动效果
			obj.rightSlide = function() {
				obj.imgs.eq(prevIdx).css({
						display: "block"
					})
					.stop().animate({
							left: -obj.imgWidth + "px",
						}, obj.duration,
						function() {
							obj.imgs.eq(prevIdx).css({
								display: "none"
							})
						})

				obj.imgs.eq(nextIdx).css({
						left: obj.imgWidth + "px",
						display: "block"
					})
					.stop().animate({
						left: 0,
					}, obj.duration)
			}

			//右点击
			obj.left.on("click", function() {
				var nowTime = Date.now();
				if (nowTime - obj.lastTime > 600) {

					console.log(nowTime + "  " + obj.lastTime)
					prevIdx = obj.currentIndex;
					obj.currentIndex--;
					if (obj.currentIndex == -1) {
						obj.currentIndex = len - 1;
					}
					nextIdx = obj.currentIndex;
					obj.lastTime = nowTime;
					obj.leftSlide();
			
					changeLtl(obj.currentIndex)
					
					clearInterval(obj.timer);
					autoPlay();
				}
			})

			//产生左滑动效果
			obj.leftSlide = function() {
				obj.imgs.eq(prevIdx).css({
						display: "block"
					})
					.stop().animate({
							left: obj.imgWidth + "px",
						}, obj.duration,
						function() {
							obj.imgs.eq(prevIdx).css({
								display: "none"
							})
						})

				obj.imgs.eq(nextIdx).css({
						left: -obj.imgWidth + "px",
						display: "block"
					}, obj.duration)
					.stop().animate({
						left: 0,
					}, obj.duration)
			}


			//自动滚动函数
			function autoPlay () {
				if (obj.autoplay) {
					obj.timer = setInterval(function() {
						//模拟右点击
						obj.right.trigger("click");
					}, obj.autoTime)
				}
			}
			

			//创建小圆点
			if (obj.point) {
				var str = "";
				for (var i = 0; i < obj.imgs.length; i++) {
					obj.point.append("<div class='little'></div>");
				}
			}

			//改变小圆点样式函数
			function changeLtl(index) {
				obj.point.children(".little").eq(index).addClass("littleActive").siblings().removeClass("littleActive")
			}

			//小圆点点击事件
			obj.point.on("click", ".little", function() {
				if ($(this).index() < obj.currentIndex) {
					//模拟左击
					prevIdx = obj.currentIndex;
					nextIdx = $(this).index();
					obj.leftSlide();
					clearInterval(obj.timer);
					obj.currentIndex = $(this).index();
					changeLtl(obj.currentIndex);
					
					autoPlay();
				} else if ($(this).index() > obj.currentIndex) {
					//模拟右击
					prevIdx = obj.currentIndex;
					nextIdx = $(this).index();
					obj.rightSlide();
					clearInterval(obj.timer);
					obj.currentIndex = $(this).index();
					changeLtl(obj.currentIndex);
					
					autoPlay();
				}
			})
			
			//放置到图片上触发
			$(this).on("mouseenter",function () {
				clearInterval(obj.timer);
			})
			
			$(this).on("mouseleave",function () {
				obj.right.trigger("click");
			})
			
		}
	})
})();
